
#import "RIBAttribute.h"

@implementation RIBAttribute


+ initialize { return [RIBAttribute setVersion:1], self; }

- init
{
  [super init];
  name = NULL;

  return self;
}

- free
{
  if (name)
  {  free(name);
  }

  return [super free];
}

// this is used only internally, for setting a copy without freeing the original
- _setName:(const char *)newName
{
  if (newName)
  {  name = strcpy((char *)malloc(1 + strlen(newName)), newName); 
  }
  else
  {  name = NULL;
  }

  return self;
}

- copyFromZone:(NXZone *)zone
{
   id   newCopy = [super copyFromZone:zone];

  [newCopy _setName:name];
  return newCopy;
}

- setName:(RtToken)newName
     n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
     printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
{  
   name = strcpy((char *)malloc(1 + strlen(newName)), newName); 
   [self setN:newN tokens:newTokens parms:newParms archiveVector:newArchiveVector printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];

   return self;
}

- (const char *)name { return name; }

- renderSelf:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  RiAttributeV(name, n, tokens, parms);

  return self;
}

- (unsigned long int)maxSampleBandwidth
{  
   unsigned long int maxSampleBandwidth = [super maxSampleBandwidth];


   if (name) { maxSampleBandwidth += 1 + strlen(name); }

   return maxSampleBandwidth;
}

- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "Attribute {%s} ", name); 
   [super writeParameterList:stream];
   return self;
}

#define typeVector "*"
#define typeValues &name

- read:(NXTypedStream*)stream 
{
    int version;
    [super read:stream];

    version = NXTypedStreamClassVersion(stream,"RIBAttribute");
    if (version == 0) NXReadTypes(stream, "i", &version), version=1;
    if (version == 1)
    {  NXReadTypes(stream, typeVector, typeValues);
    } 
    else 
    {
    }
    return self;
}

- write:(NXTypedStream*)stream 
{
    [super write:stream];

    NXWriteTypes(stream, typeVector, typeValues);

    return self;
}


@end
